﻿using A.M.ETO;
using AutoMapper;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using Newtonsoft.Json;
using Public_Class_Library;
using Q.M.Finance.Domain.Entity;
using Q.M.Sell.Application.IServices;
using Q.M.Sell.Doamin.DTO;
using Q.M.Sell.Doamin.Entity;
using Q.M.Sell.Infrastruecure.IRepository;
using Q.M.Sell.Infrastruecure.Repository;
using Redis_ClientInfor;
using System.Linq.Expressions;
using static StackExchange.Redis.Role;

namespace Q.M.Sell.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SalesWriteController : ControllerBase
    {
        private readonly ISalesServices salesServices;
        private readonly ICapPublisher capPublisher;
        private readonly Cross_service_Helper cross_Service_Helper;
        public SalesWriteController(ISalesServices salesServices, Cross_service_Helper cross_Service_Helper, ICapPublisher capPublisher)
        {
            this.salesServices = salesServices;
            this.cross_Service_Helper = cross_Service_Helper;
            this.capPublisher = capPublisher;
        }
        /// <summary>
        /// 添加销售订单
        /// </summary>
        /// <param name="salesAddDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetsalesAddInfo(salesAddDTO salesAddDTO)
        {
            var res =await salesServices.GetsalesAddInfo(salesAddDTO);
            return Ok(res);
        }

        [HttpGet]
        [CapSubscribe("GetXsName")]
        public async Task<IActionResult> GetAddXsInfor(string? id)
        {
            var res = await salesServices.GetAddXsInfor(id);
            return Ok(res);
        }

        /// <summary>
        /// 先获取所有的审批
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProccInfir()
        {
            var res = "http://10.31.59.5:8002/api/Approval_form/GetappList";
            var mes = await cross_Service_Helper.Get(res);
            var ges = JsonConvert.DeserializeObject<List<AddStaffInforDTO>>(mes);
            var list = ges.Where(c => c.approval_name == "销售系统").FirstOrDefault();
            return Ok(list.apprpvalTwoDTO);
        }

        /// <summary>
        /// 审批的消息队列
        /// </summary>
        /// <param name="addStaffInforDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddInforMQ(AprInforGerDTO addStaffInforDTO)
        {
            //申请成功需要传一个消息队列;
            capPublisher.Publish("addStaffInforDTOGer", addStaffInforDTO);
            return Ok("");
        }


        /// <summary>
        /// 修改销售订单
        /// </summary>
        /// <param name="salesAddDTO"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> GetsalesUpdateInfo(salesUpdateDTO salesUpdateDTO)
        {
            var res = await salesServices.GetsalesUpdateInfo(salesUpdateDTO);
            return Ok(res);
        }
        /// <summary>
        /// 删除信息订单
        /// </summary>
        /// <param name="salesid"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Deletesales(string? salesid)
        {
            var res = await salesServices.Deletesales(salesid);
            return Ok(res);
        }
        /// <summary>
        /// 销售订单显示+分页+查询
        /// </summary>
        /// <param name="salesid"></param>
        /// <param name="stock_removal"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetsalesInfo(string? salesid, string? stock_removal, int pageIndex, int pageSize)
        {
            var list = salesServices.GetsalesInfo(salesid, stock_removal, pageIndex, pageSize);
            return Ok(list);
        }
        /// <summary>
        /// 反填销售订单
        /// </summary>
        /// <param name="salesid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> FindsalesInfo(string? salesid)
        {
            var list = await salesServices.FindsalesInfo(salesid);
            return Ok(list);

        }
        /// <summary>
        /// Redis测试
        /// </summary>
        /// <returns></returns>
        [Route(template: "Redis")]
        [HttpGet]
        public ActionResult Redis()
        {
            var redisResult = RedisClient.redisClient.SetStringKey("qql", "456");
            return Ok(new { code = "200", res = new { msg = redisResult.ToString() } });
        }
        /// <summary>
        /// 枚举转下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object Getmeans_of_payments()
        {
            return Enum.GetValues<means_of_payments>().Select(x => new
            {
                id = (int)x,
                name=x.ToString(),
            });
        }
        /// <summary>
        /// 跨服务调用接口
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(string? Url)
        {
            var res = await cross_Service_Helper.Get(Url);
            return Ok(res);
        }

        /// <summary>
        /// 获取产品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProoInforShow(int page,int size)
        {
            var res =  cross_Service_Helper.Get("http://10.31.59.21:8087/api/Product_management_table/GetQshowinfor").Result;
            var obj = JsonConvert.DeserializeObject<List<QPromInfor>>(res);


            var  obj1 = cross_Service_Helper.Get("http://10.31.59.5:8001/api/Table_of_unitsRead/GetTableUrl").Result;
            List<table_of_units>? jsonObj = JsonConvert.DeserializeObject<List<table_of_units>>(obj1);
            var list = (from a in obj
                        join b in jsonObj
                        on a.unit_id equals b.unit_number
                        select new QPromInfor
                        {
                            product_number = a.product_number,
                            unit_name = b.unit_name,
                            product_name = a.product_name,
                            specification_model = a.specification_model,
                            unit_id = a.unit_id,
                            unit_sales_price = a.unit_sales_price,
                            selling_number = a.selling_number,
                            discount = a.discount,
                            number=1
                        }).ToList();
            PageETO<QPromInfor> pageETO = new PageETO<QPromInfor>();
            pageETO.TotalCount = list.Count();
            pageETO.PageCount = (int)(list.Count() * 1.0 /size);
            pageETO.Data = list.Skip((page-1)*size).Take(size).ToList();
            return Ok(pageETO);
        }
        /// <summary>
        /// 根据销售单号反填付款方信息
        /// </summary>
        /// <param name="salesid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInfo(string salesid)
        {
            var list = salesServices.GetInfo(salesid);
            return Ok(list);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportTiExcel()
        {
            var list = salesServices.GetShowInfor();
            var steram = new MemoryStream();
            steram.SaveAs(list);
            steram.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(steram, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "销售信息.xlsx"
            };
        }
    }
}
